<?php

declare(strict_types=1);

namespace app;


use think\Model;
use think\model\concern\SoftDelete;

class BaseModel extends Model
{
    use SoftDelete;

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = true;
    //软删除
    protected $deleteTime = 'delete_time';
    protected $hidden = ['delete_time'];


    /**
     * 查询一条数据
     *
     * @param array $where 查询条件
     * @param string $field 查询字段
     * @param array $with 关联预载入
     * @param array $withJoin join关联
     * @param string|array $order 排序
     * @param bool $isSqlMode 是否打印SQL语句
     * @param bool $isDelete 是否查询软删除数据
     *
     * @return mixed
     */
    public function getOne(
        array $where = [],
        string $field = '*',
        array $with = [],
        array $withJoin = [],
        string|array $order = 'id desc',
        bool $isSqlMode = false,
        bool $isDelete = false,
    ): mixed {
        $model = new static();
        $data  = $model->fieldRaw($field);
        if ( ! empty($where)) {
            $data = $data->where($where);
        }

        if ( ! empty($withJoin)) {
            $data = $data->withJoin($withJoin, $with['connect']);
        }

        // 添加关联查询
        if ( ! empty($with)) {
            $data = $data->with($with);
        }
        $data = $data->orderRaw($order);
        if ($isSqlMode) {
            return $data->fetchSql(true)->find();
        } else {
            if ($isDelete) {
                return $data->withTrashed()->find();
            } else {
                return $data->find();
            }
        }
    }


    /**
     * 查询多条数据
     *
     * @param array $where
     * @param string $field
     * @param array $with
     * @param array $withJoin
     * @param string|array $order
     * @param int $page
     * @param int $pageSize
     * @param bool $needCount
     * @param bool $isSqlMode
     * @param bool $isDelete
     *
     * @return array
     */
    public function getList(
        array $where = [],
        string $field = '*',
        array $with = [],
        array $withJoin = [],
        string|array $order = 'id desc',
        int $page = 0,
        int $pageSize = 0,
        bool $needCount = false,
        bool $isSqlMode = false,
        bool $isDelete = false,
    ): array {
        $model = new static();
        $data  = $model->field($field);
        if ( ! empty($withJoin)) {
            $data = $data->withJoin($withJoin,'left');
        }

        // 添加关联查询
        if ( ! empty($with)) {
            $data = $data->with($with);
        }

        // 添加查询条件
        if ( ! empty($where)) {
            $data = $data->where($where);
        }

        // 计算总数（如果需要）
        $count = 0;
        if ($needCount) {
            if ($isSqlMode) {
                $count = $data->fetchSql(true)->count();
            } else {
                if ($isDelete) {
                    $count = $data->withTrashed()->count();
                } else {
                    $count = $data->count();
                }
            }
        }

        // 添加排序条件
        if ( ! empty($order)) {
            $data = $data->orderRaw($order);
        }

        // 应用分页
        if ($page > 0 && $pageSize > 0) {
            $data = $data->page($page, $pageSize);
        }

        // 执行查询并封装结果
        if ($isSqlMode) {
            $data = $data->fetchSql(true)->select();
        } else {
            if ($isDelete) {
                $data = $data->withTrashed()->select()->toArray();
            } else {
                $data = $data->select()->toArray();
            }
        }

        return [
            'data'  => $data,
            'total' => $count,
        ];
    }
}